<template>
  <div class="bigbox">
    <div class="topbox" :style="{ height: isheight + 'px' }">
      <el-form
        :model="form"
        ref="form"
        label-position="left"
        label-width="80px"
        onsubmit="return false"
      >
        <el-col :span="6">
          <el-form-item label="公司名称">
            <el-input v-model="orgName" placeholder="请输入公司名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6" />
        <el-col :span="6" />
        <el-col :span="6">
          <el-form-item>
            <div>
              <button @click="chongzhi" class="button">重置</button>
              <button @click="submit" class="button chaxun">查询</button>
            </div>
          </el-form-item>
        </el-col>
      </el-form>
    </div>
    <div class="bodybox">
      <div class="titlebox">
        <div class="text">公司注册信息</div>
        <div
          class="right"
          :style="{ alignItems: 'center', lineHeight: '32.5px' }"
        >
          <button
            type="primary"
            size="small"
            class="bewdao daochu"
            style="margin-right: 10px; padding: 0 10px"
            v-if="isShow"
            @click="add(0)"
          >
            +新增合作伙伴
          </button>
          <button
            type="primary"
            size="small"
            style="padding: 0 10px"
            class="bewdao daochu"
            @click="add(1)"
          >
            +新增分公司
          </button>
          <el-tooltip class="item" effect="dark" content="刷新" placement="top">
            <span
              @click="shuaxin"
              :style="{ cursor: 'pointer', fontSize: '20px' }"
              ><i class="el-icon-refresh-right"></i
            ></span>
          </el-tooltip>
          <el-dropdown trigger="click" @command="command">
            <span class="el-dropdown-link">
              <el-tooltip
                class="item"
                effect="dark"
                content="密度"
                placement="top"
              >
                <span :style="{ cursor: 'pointer', fontSize: '20px' }"
                  ><i class="el-icon-d-caret"></i
                ></span>
              </el-tooltip>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item class="dropdown-list" command="默认"
                >默认</el-dropdown-item
              >
              <el-dropdown-item class="dropdown-list" command="中等"
                >中等</el-dropdown-item
              >
              <el-dropdown-item class="dropdown-list" command="紧凑"
                >紧凑</el-dropdown-item
              >
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <div class="table-content">
        <template>
          <el-table
            v-loading="loading"
            :data="tableData"
            style="width: 100%"
            border
            id="exportTabyong"
            ref="table"
            :size="iissize"
            :header-cell-style="{
              background: '#f5f5f5',
              'text-align': 'center'
            }"
          >
            <template slot="empty">
              <el-empty description="暂无数据"></el-empty>
            </template>
            <!-- :cell-style="{ 'text-align': 'center' }" -->
            <el-table-column type="index" label="序号" width="50">
            </el-table-column>
            <el-table-column prop="orgName" label="公司名称" width="230">
              <template slot-scope="scope">
                <div class="orgName" v-if="scope.row.orgType == '30'">
                  <img class="orgName-img1" src="@/assets/image/分公司.png" />{{
                    scope.row.orgName
                  }}
                </div>
                <div class="orgName" v-if="scope.row.orgType == '03'">
                  <img
                    class="orgName-img2"
                    src="@/assets/image/合作伙伴.png"
                  />{{ scope.row.orgName }}
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="address" label="公司地址" width="300">
            </el-table-column>
            <el-table-column prop="contacts" align="center" label="联系人">
            </el-table-column>
            <el-table-column
              prop="contactsMobile"
              align="center"
              label="联系方式"
              width="160"
            >
            </el-table-column>
            <el-table-column
              prop="applyDate"
              align="center"
              label="申请时间"
              width="100"
            >
            </el-table-column>
            <el-table-column prop="businessScope" label="业务类型" width="300">
              <template slot-scope="scope">
                {{
                  scope.row.businessScope
                    ? scope.row.businessScope
                        .split(',')
                        .map((scope) =>
                          (found = YWFW.find((item) => item.code === scope))
                            ? found.name
                            : ''
                        )
                        .join(',')
                    : ''
                }}
              </template>
            </el-table-column>
            <el-table-column
              prop="manageUserNo"
              align="center"
              label="账号"
              width="150"
            >
            </el-table-column>
            <!-- <el-table-column prop="industryTypeName" label="密码" width="200">
            </el-table-column> -->
            <el-table-column prop="status" align="center" label="状态">
              <template slot-scope="scope">
                {{ statusList.find((i) => scope.row.status === i.value)?.text }}
              </template>
            </el-table-column>
            <el-table-column fixed="right" label="操作" width="130">
              <template slot-scope="scope">
                <div class="operate">
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="预览"
                    placement="top"
                  >
                    <i
                      style="
                        font-size: 14px;
                        color: rgb(64, 149, 229);
                        cursor: pointer;
                      "
                      class="el-icon-view"
                      @click="preview(scope.row)"
                    ></i
                  ></el-tooltip>
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="编辑"
                    placement="top"
                  >
                    <i
                      style="
                        font-size: 14px;
                        color: rgb(64, 149, 229);
                        cursor: pointer;
                      "
                      class="el-icon-edit"
                      @click="handleEdit(scope.row)"
                    ></i
                  ></el-tooltip>
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="删除"
                    placement="top"
                  >
                    <i
                      style="font-size: 14px; color: red; cursor: pointer"
                      class="el-icon-delete"
                      @click="handleDel(scope.row.id)"
                    ></i
                  ></el-tooltip>
                  <el-tooltip
                    class="item"
                    effect="dark"
                    content="授权"
                    placement="top"
                  >
                    <i
                      style="font-size: 14px; color: red; cursor: pointer"
                      class="el-icon-medal"
                      @click="handleSq(scope.row)"
                    ></i
                  ></el-tooltip>
                </div>
              </template>
            </el-table-column>
          </el-table>
        </template>
        <div class="fenye">
          <el-pagination
            background
            layout="prev, pager, next"
            :current-page.sync="pageobj._pageNo"
            @current-change="getdatalist(orgName)"
            :total="total"
          >
          </el-pagination>
        </div>
      </div>
    </div>
    <div class="dialog">
      <el-dialog
        :title="'新增公司注册信息'"
        :visible.sync="centerDialogVisible"
        label-position="left"
        width="800px"
        :destroy-on-close="true"
        :close-on-click-modal="false"
        :before-close="beforeClose"
      >
        <div class="top-line">
          <div class="top-item">
            <div class="circle" :class="progress == 1 ? 'checked-bgc' : ''">
              1
            </div>
            <div class="text">账号信息</div>
          </div>
          <div class="line"></div>
          <div class="top-item">
            <div class="circle" :class="progress == 2 ? 'checked-bgc' : ''">
              2
            </div>
            <div class="text">业务范围</div>
          </div>
          <div class="line"></div>
          <div class="top-item">
            <div class="circle" :class="progress == 3 ? 'checked-bgc' : ''">
              3
            </div>
            <div class="text">公司信息</div>
          </div>
          <div class="line"></div>
          <div class="top-item">
            <div class="circle" :class="progress == 4 ? 'checked-bgc' : ''">
              4
            </div>
            <div class="text">地理位置</div>
          </div>
        </div>
        <div class="content">
          <AccountInfo
            ref="AccountInfo"
            @next="next"
            @registerData="registerData"
            :addType="addType"
            :userInfo="userInfo"
            v-if="progress == 1 && centerDialogVisible"
          />
          <BusinessScope
            @next="next"
            :id="id"
            :isEdit="isEdit"
            ref="BusinessScope"
            v-else-if="progress == 2 && centerDialogVisible"
          />
          <CompanyInfo
            @next="next"
            :id="id"
            :isEdit="isEdit"
            :companyData="companyData"
            ref="CompanyInfo"
            v-else-if="progress == 3 && centerDialogVisible"
          />
          <Amap
            ref="amap"
            :id="id"
            :dizhi="dizhi"
            :isEdit="isEdit"
            @finish="finish"
            v-else-if="progress == 4 && centerDialogVisible"
          />
        </div>
      </el-dialog>
      <el-dialog
        :title="'公司注册信息'"
        :visible.sync="centerDialogVisible1"
        label-position="left"
        width="900px"
        :destroy-on-close="true"
        :close-on-click-modal="false"
        :before-close="beforeClose"
      >
        <div class="content">
          <AccountInfo
            ref="AccountInfo"
            :id="id"
            @next="next"
            @registerData="registerData"
            :addType="addType"
            :isPreview="isPreview"
            :companyData="companyData"
            :userInfo="userInfo"
            v-if="centerDialogVisible1"
          />
          <BusinessScope
            @next="next"
            :id="id"
            :isPreview="isPreview"
            :companyData="companyData"
            :isEdit="isEdit"
            ref="BusinessScope"
            v-if="centerDialogVisible1"
          />
          <CompanyInfo
            @next="next"
            :id="id"
            :isPreview="isPreview"
            :isEdit="isEdit"
            :companyData="companyData"
            ref="CompanyInfo"
            v-if="centerDialogVisible1"
          />
          <Amap
            ref="amap"
            :id="id"
            :dizhi="dizhi"
            :isPreview="isPreview"
            :isEdit="isEdit"
            :companyData="companyData"
            @finish="finish"
            v-if="centerDialogVisible1"
          />
        </div>
      </el-dialog>
    </div>
    <nt-dialog
      :visible.sync="sqVisible"
      v-if="sqVisible"
      width="900px"
      @diaClose="diaClose"
    >
      <Authorize
        @sqSave="sqSave"
        :id="sqId"
        :companyData="sqData"
        ref="Authorize"
      />
    </nt-dialog>
  </div>
</template>

<script>
// import { saveAs } from 'file-saver'
import {
  // delCustomerFileApi,
  getDictionaryApi
} from '../../api/customerFile'
import {
  getBusinessApi,
  getCompanyRegisterApi,
  getEnterpriseData,
  getInfoDataApi,
  getStatusValueApi,
  getUserNoApi,
  isAddApi,
  delCompanyRegisterApi
} from '../../api/companyRegister.js'
import AccountInfo from './components/AccountInfo.vue'
import BusinessScope from './components/BusinessScope.vue'
import CompanyInfo from './components/CompanyInfo.vue'
import Authorize from './components/Authorize.vue'
import Amap from './components/Amap.vue'
import NtDialog from '@/components/nt-dialog.vue'
export default {
  name: 'ConfigureIndex',
  components: {
    AccountInfo,
    BusinessScope,
    CompanyInfo,
    Amap,
    NtDialog,
    Authorize
  },
  data() {
    return {
      // lng: '',
      // lat: '',
      // address: '',
      orgName: '',
      isEdit: false,
      isPreview: false,
      dizhi: '',
      centerDialogVisible: false,
      centerDialogVisible1: false,
      form: { alarmLevel: '0' },
      total: 0,
      isheight: 72,
      progress: 1,
      iissize: 'medium',
      loading: false,
      tableData: [],
      pageobj: {
        _pageNo: 0,
        _pageSize: 10
      },
      userInfo: {},
      addType: 0,
      YWFW: [],
      isShow: false,
      sqVisible: false,
      statusList: [],
      companyData: {},
      sqId: ''
    }
  },
  watch: {},
  async created() {
    await Promise.all([
      this.isAdd(),
      this.getStatusValue(),
      this.getUserNo(),
      this.getBusiness()
    ])
  },

  async mounted() {
    await this.getchushihua()
  },

  methods: {
    async sqSave() {
      this.sqVisible = false
      await this.getdatalist(this.orgName)
    },
    // 授权
    handleSq(row) {
      this.sqId = row.id
      this.sqData = row
      console.log(row)
      this.sqVisible = true
    },
    diaClose() {
      this.sqVisible = false
    },
    async getBusiness() {
      const res = await getBusinessApi()
      this.YWFW = res.data.list
    },
    async isAdd() {
      const res = await isAddApi()
      this.isShow = res.data.entity
    },
    async getStatusValue() {
      const res = await getStatusValueApi()
      console.log(res.data.list)
      this.statusList = res.data.list
    },
    async getUserNo() {
      const res = await getUserNoApi()
      console.log(res.data.entity)
      this.userInfo = res.data.entity
    },
    // 初始化
    async getchushihua() {
      this.getdatalist('')
    },
    // 获取数据
    async getdatalist(now) {
      this.loading = true
      const res = await getCompanyRegisterApi({
        _pageNo: this.pageobj._pageNo - 1,
        _pageSize: this.pageobj._pageSize,
        orgName: now
      })
      console.log(res.data?.list)
      this.tableData = res.data?.list
      this.total = res.data?.totalCount
      this.loading = false
    },
    // 刷新列表
    async shuaxin() {
      // 确保业务类型数据已加载
      if (!this.YWFW.length) {
        await this.getBusiness()
      }
      await this.getdatalist(this.orgName)
      this.$message.success('列表刷新成功')
    },
    async submit() {
      // 确保业务类型数据已加载
      if (!this.YWFW.length) {
        await this.getBusiness()
      }
      await this.getdatalist(this.orgName)
    },
    command(val) {
      if (val === '默认') {
        this.iissize = 'medium'
      } else if (val === '中等') {
        this.iissize = 'small'
      } else {
        this.iissize = 'mini'
      }
    },
    async chongzhi() {
      // 确保业务类型数据已加载
      if (!this.YWFW.length) {
        await this.getBusiness()
      }
      await this.getchushihua()
      await this.getdatalist('')
      this.form.alarmLevel = '0'
    },
    next(progress, id) {
      this.progress = progress
      if (id) {
        this.id = id
      }
    },
    registerData(data) {
      this.companyData = data
      this.dizhi = data.companyAddress
    },
    async beforeClose() {
      this.centerDialogVisible = false
      this.centerDialogVisible1 = false
      this.progress = 1
      this.id = ''
      this.isEdit = false
      this.isPreview = false
      await this.shuaxin()
    },
    async finish() {
      this.centerDialogVisible = false
      this.progress = 1
      this.id = ''
      await this.getdatalist(this.orgName)
    },
    // 编辑
    async handleEdit(row) {
      console.log(row)
      if (row.status == 4) return this.$message('创建已完成，不可编辑')
      this.progress = +row.status + 1
      this.id = row.id
      if (row.status == 2) {
        const res = await getEnterpriseData(row.orgName)
        // console.log(res.data.data.data)
        this.companyData = res.data.data.data
        this.dizhi = res.data.data.data.companyAddress
      } else {
        this.dizhi = row.address
      }
      this.isEdit = true
      this.centerDialogVisible = true
    },
    async preview(row) {
      console.log(row)
      this.isPreview = true
      this.id = row.id
      const res = await getInfoDataApi({ id: row.id })
      // console.log(res.data.entity)
      this.companyData = res.data.entity
      this.centerDialogVisible1 = true
    },
    // 添加
    async add(type) {
      this.addType = type
      // this.bizOrgName2 = this.bizOrgName
      // console.log(this.tableData)
      this.centerDialogVisible = true
      // this.dialogForm = {}
      // const newTime = new Date()
      // console.log(newTime)
      // this.$set(this.dialogForm, 'createDate', newTime)
      // this.$refs.dialogForm?.resetFields()
    },
    // 删除
    async handleDel(id) {
      console.log(id)
      this.$confirm('此操作将永久删除该档案, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const res = await delCompanyRegisterApi(id)
          this.$message.success(res.data.content)
          this.getdatalist(this.orgName)
        })
        .catch((action) => {
          if (action === 'cancel') {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          }
        })
    },
    // 编辑保存
    async onSubmit() {
      console.log('wancheng')
    },
    async getIndustryType(industryTypeName) {
      const resp = await getDictionaryApi(
        'GWYXDM_HYFL',
        this.dialogForm.industryCategory
      )
      console.log(resp)
      this.HYFL = resp.data.list
      this.HYFL.shift()
      this.$set(this.dialogForm, 'industryTypeName', industryTypeName || '')
      this.dialogForm.industryCategory = this.HYLB.find(
        (item) => item.value === this.dialogForm.industryCategoryName
      )?.id
    }
    // 重置弹窗里的表单
    // resetForm () {
    //   this.$refs.dialogForm.resetFields()
    //   this.$refs.amap.form.lng = ''
    //   this.$refs.amap.form.lat = ''
    //   this.$refs.amap.form.keywords = ''
    //   this.$refs.amap.removeMarker()
    // }
  }
}
</script>

<style lang="scss" scoped>
.option {
  height: auto;
}
.bigbox {
  background-color: #f1f1f1;
  padding: 10px;
  .topbox {
    background-color: #fff;
    padding: 20px 20px 0 20px;
    height: 80px !important;
    box-sizing: border-box;
    width: 100% !important;
    ::v-deep .el-form-item {
      width: 100%;
      margin-left: 0;
    }
    .button {
      width: 63px;
      height: 32px;
      margin-right: 5px;
      background-color: #fff;
      border: 1px solid #ccc;
      border-radius: 3px;
    }
    .chaxun {
      background-color: #1890ff;
      color: #fff;
    }
  }
  .bodybox {
    height: 90%;
    background-color: #fff;
    margin-top: 10px;
    padding: 20px 20px 20px 20px;
    .titlebox {
      display: flex;
      justify-content: space-between;
      margin-bottom: 10px;
      .text {
        font-weight: bold;
        font-size: 18px;
      }
      .right {
        align-items: center;
        i {
          margin-left: 10px;
        }
        .bewdao {
          height: 32px;
        }
        .daochu {
          background-color: #1890ff;
          color: #fff;
          border: none;
          border-radius: 3px;
        }
      }
    }
    .fenye {
      margin-top: 15px;
      text-align: right;
    }
  }
}

::v-deep .el-form-item__content {
  width: calc(100% - 100px);
}

::v-deep .el-col-6 {
  height: 42px;
  text-align: right;
}
::v-deep .el-input__inner {
  height: 37px;
}
::v-deep.el-form--inline {
  height: 100% !important;
  box-sizing: border-box;
}
::v-deep .el-input__icon {
  line-height: 20px;
}

.dropdown-list {
  padding: 0;
  margin: 0;
  text-align: center;
  width: 50px;
}
.operate {
  display: flex;
  justify-content: space-around;
}
// .dialog {
//   .content {
//     padding-top: 30px;
//   }
// }
.dialogBtn {
  width: 150px;
  height: 40px;
  border-radius: 4px;
  font-size: 14px;
  text-align: center;
  align-items: center;
}

::v-deep.el-date-editor {
  .el-range-separator {
    line-height: 22px;
  }
}

.top-line {
  display: flex;
  width: 100%;
  height: 80px;
  padding-bottom: 20px;
  margin-bottom: 30px;
  justify-content: space-between;
  line-height: 30px;
  align-items: center;
  border-bottom: 1px solid #bbb;
  .line {
    width: 12%;
    height: 1px;
    border: 1px solid #e0e0e0;
  }
  .top-item {
    display: flex;
    .circle {
      width: 30px;
      height: 30px;
      border-radius: 50%;
      border: 1px solid #bbb;
      color: #bbb;
      text-align: center;
      line-height: 30px;
      margin-right: 10px;
    }
    .text {
      font-weight: 800;
    }
  }
}
.checked-bgc {
  background-color: #1684fc;
  color: #fff !important;
  border: 1px solid #1684fc !important;
}
.orgName {
  .orgName-img1 {
    width: 20px;
    height: 20px;
    margin-bottom: -4px;
  }
  .orgName-img2 {
    width: 15px;
    height: 15px;
    margin-right: 5px;
    margin-bottom: -2px;
  }
}
</style>
